import {serveImg} from "@/api/user/index.js"
/**
 * list转bese64
 * @param {*} userList 
 * @param {*} url 
 * @returns 
 */
export async function showImgBase64(userList,url){
  const data1 = await Promise.all(
    Array.from(userList).map(async (item) => {
      let result = "";
      try {
        const res = await serveImg(url, true);
        const base64 = Buffer.from(res, 'binary').toString('base64');
        result = `data:image/jpeg;base64,${base64}`; // 返回 Base64 URL 
      } catch (error) {
        console.error("Failed to load image:", error);
      }
      return { ...item, url: result };
    })
  );
  return data1;
}

/**
 * list转Blob
 * @param {*} userList 
 * @param {*} url 
 * @returns 
 */
export async function showImgBlob(userList,url){
  const data1 = await Promise.all(
    Array.from(userList).map(async (item) => {
      let result = "";
      try {
        const res = await serveImg(url, true);
        const blob = new Blob([res], { type: "image/jpeg" }); // 确保 type 正确

        result = URL.createObjectURL(blob);
        // 使用 btoa 替代 Buffer

      } catch (error) {
        console.error("Failed to load image:", error);
      }
      return { ...item, url: result };
    })
  );
  return data1;
}